package com.demoweb.flappy.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demoweb.model.dto.Membership;

@WebServlet("/flappy/mail/sendmail.action")
public class SendMailServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
				
		String subject = req.getParameter("subject");
		String content = req.getParameter("content");
		String from = ((Membership)req.getSession().getAttribute("loginuser")).getMemberId();
		String to = req.getParameter("to");
		String cc = "dltjdn8903@naver.com";
		
		try {
		//연결 설정
		Properties p = new Properties();
		p.put("mail.smpt.host", "127.0.0.1");
		//연결
		Session session = Session.getDefaultInstance(p, null);
		
		//전송할 메일 정보 객체 생성
		MimeMessage message = new MimeMessage(session);
		
		//보낸 메일
		message.setFrom(new InternetAddress(from));
		//받는 메일
		message.setRecipients(MimeMessage.RecipientType.TO, to);
		//참조 메일
		message.setRecipients(MimeMessage.RecipientType.CC, cc);
		//제목
		message.setSubject(subject);
		//내용
		message.setText(content);
		
		//메일 전송
		Transport.send(message);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		resp.sendRedirect("/flappy/mail/sendmailform.action");
		
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}










